package cc.linker.steplink.crm.merchanth5.service.impl;

import cc.linker.steplink.crm.merchanth5.dao.acs.PromoterClueDao;
import cc.linker.steplink.crm.merchanth5.pojo.acs.PromoterCluePo;
import cc.linker.steplink.crm.merchanth5.service.acs.PromoterClueService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@EnableAsync
@Service
public class PromoterClueServiceImpl implements PromoterClueService {
    @Autowired
    private PromoterClueDao promoterClueDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<PromoterCluePo> getList(String promoterId, Integer type) {
        return promoterClueDao.getList(promoterId, type);
    }

    @Override
    public List<PromoterCluePo> getCustomerClueList(String promoterId, String customerId, Integer type) {
        return promoterClueDao.getCustomerClueList(promoterId, customerId, type);
    }

    @Override
    public Map getCustomerInfo(String promoterId, String customerId) {
        return promoterClueDao.getCustomerInfo(promoterId, customerId);
    }

    @Async
    @Override
    public void addClue(Map<String, Object> params) {
        LOGGER.info("线索参数===========" + JSON.toJSONString(params));
        if (StringUtils.isEmpty(params.get("promoterId"))) {
            return;
        }
        String promoterId = params.get("promoterId").toString();
        //判断是否是加盟代理
        Integer count = promoterClueDao.getPromoterCount(promoterId);
        if (count == null || count == 0) {
            return;
        }
        if (promoterId.equals(params.get("customerId").toString())) {
            return;
        }
        Integer type = Integer.valueOf(params.get("type").toString());
        //动作类型 1.浏览商城 2.浏览商品详情 3浏览知识商品详情 4报名详情 5表单详情 6浏览权益卡详情
        // 7领取卡券 8加入购物车 9购买商品 10购买知识商品 11报名付费 12表单付费 13开通权益卡'
        Integer actionType = Integer.valueOf(params.get("actionType").toString());
        String actionContent = "";
//        Integer isPromot = 0;
        if (type == 1) {
            //线索
            //1.浏览商城 2.浏览商品详情 3浏览知识商品详情 4报名详情 5表单详情 6浏览权益卡详情  8加入购物车
            switch (actionType) {
                case 1:
//                    isPromot = 1;
                    actionContent = "浏览商城首页";
                    break;
                case 2:
                case 3:
                    //商品和知识商品
                    //判断分销
//                     isPromot = promoterClueDao.getIsPromotByGoods(params);
                    actionContent = "浏览" + params.get("relevantName");
                    break;
                case 4:
                    //报名
                    //判断分销
//                     isPromot = promoterClueDao.getIsPromotByEnroll(params);
                    actionContent = "浏览" + params.get("relevantName");
                    break;
                case 5:
                    //表单
                    //判断分销
//                    Map param = new HashMap();
//                    param.put("formId",params.get("relevantId"));
//                    FormLoginDto formLoginDto= FormLoginDto.builder()
//                            .customerPhone("")
//                            .customerId(params.get("customerId").toString())
//                            .orgId(params.get("orgId").toString())
//                            .customerName("")
//                            .storeName("").build();
//                    Map<String,Object> customerRecord = (Map) this.formRequest.doRequest("/pc/form/templates/getByFormId", param, formLoginDto);
//                    if(!StringUtils.isEmpty(customerRecord.get("promotion"))){
//                         isPromot = Integer.valueOf(customerRecord.get("promotion").toString());
//                    }
                    actionContent = "浏览" + params.get("relevantName");
                    break;
                case 6:
                    //权益卡
                    //判断分销
//                    isPromot = promoterClueDao.getIsPromotByVip(params);
                    actionContent = "浏览" + params.get("relevantName");
                    break;
                case 7:
//                    isPromot = 1;
                    actionContent = "领取" + params.get("relevantName");
                    break;
                case 8:
                    //商品和知识商品
                    //判断分销
//                    isPromot = promoterClueDao.getIsPromotByGoods(params);
                    actionContent = "将" + params.get("relevantName") + "加入购物车";
                    break;
                default:
                    actionContent = "其他";
                    break;
            }
        } else {
            //成交
            // 7领取卡券 9购买商品 10购买知识商品 11报名付费 12表单付费 13开通权益卡'
            switch (actionType) {
                case 9:
                case 10:
                    //商品和知识商品
                    //判断分销
//                    isPromot = promoterClueDao.getIsPromotByGoods(params);
                    actionContent = "购买了" + params.get("relevantName");
                    break;
                case 11:
                    //报名
                    //判断分销
//                    isPromot = promoterClueDao.getIsPromotByEnroll(params);
                    actionContent = "报名" + params.get("relevantName");
                    break;
                case 12:
                    //表单
                    //判断分销
//                    Map param = new HashMap();
//                    param.put("formId",params.get("relevantId"));
//                    FormLoginDto formLoginDto= FormLoginDto.builder()
//                            .customerPhone("")
//                            .customerId(params.get("customerId").toString())
//                            .orgId(params.get("orgId").toString())
//                            .customerName("")
//                            .storeName("").build();
//                    Map<String,Object> customerRecord = (Map) this.formRequest.doRequest("/pc/form/templates/getByFormId", param, formLoginDto);
//                    if(!StringUtils.isEmpty(customerRecord.get("promotion"))){
//                        isPromot = Integer.valueOf(customerRecord.get("promotion").toString());
//                    }
                    actionContent = "报名" + params.get("relevantName");
                    break;
                case 13:
                    //权益卡
                    //判断分销
//                    isPromot = promoterClueDao.getIsPromotByVip(params);
                    actionContent = "开通了" + params.get("relevantName");
                    break;
                default:
                    actionContent = "其他";
                    break;
            }
        }
//        if(isPromot==null || isPromot!=1){
//            //不是分销不记录
//            return;
//        }
        if (type == 2) {
            //需要查寻下头像和昵称
            Map customerMap = promoterClueDao.getCustomerMap(params);
            params.putAll(customerMap);
        }
        params.put("actionContent", actionContent);

        if (StringUtils.isEmpty(params.get("nickName"))) {
            String promoterClueNums = stringRedisTemplate.opsForValue().get("promoterClueNum");
            String num;
            Map map;
            if (StringUtils.isEmpty(promoterClueNums)) {
                map = new HashMap();
                map.put(params.get("customerId").toString(), "1");
                num = "1";
            } else {
                map = (Map) JSON.parse(promoterClueNums);
                if (!StringUtils.isEmpty(map.get(params.get("customerId").toString()))) {
                    num = map.get(params.get("customerId").toString()).toString();
                } else {
                    num = map.size() + 1 + "";
                    map.put(params.get("customerId").toString(), num);
                }
            }
            params.put("nickName", "匿名用户" + num);
            stringRedisTemplate.opsForValue().set("promoterClueNum", JSON.toJSONString(map));
        }
        if (StringUtils.isEmpty(params.get("cover"))) {
            Map customerMap = promoterClueDao.getCustomerMap(params);
            params.put("cover", customerMap.get("cover"));
        }
        promoterClueDao.addClue(params);
    }
}
